﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;

using System.Text;
using System.Windows.Forms;
using DAO;
using DTO;
using DataGridViewAutoFilter;

namespace ProductManagement
{
    public partial class frmQLPhanPhoi : Form
    {
        //DataTable dtGasXuatKho, dtGasPhanPhoi;
        public frmQLPhanPhoi()
        {
            InitializeComponent();
        }
       
        private void LoadDaiLy()
        {
            String strSql;
            if ((int)cboDotGiaoHang.SelectedValue == 0)
                strSql = "select dl.ID, dl.TenDaiLy from DaiLy dl inner join PhanPhoi pp on pp.DaiLy = dl.ID group by dl.ID, dl.TenDaiLy";
            else
                strSql = "select dl.ID, dl.TenDaiLy from DaiLy dl inner join PhanPhoi pp on pp.DaiLy = dl.ID where pp.MaGiaoHang = "+(int)cboDotGiaoHang.SelectedValue +" group by dl.ID, dl.TenDaiLy";
            DataTable dt = DataAccess.Query(strSql);
            DataRow newRow = dt.NewRow();
            newRow["ID"] = 0;
            newRow["TenDaiLy"] = "Tất cả";
            dt.Rows.InsertAt(newRow, 0);
            cboDaiLy.DisplayMember = "TenDaiLy";
            cboDaiLy.ValueMember = "ID";
            cboDaiLy.DataSource = dt;
            cboDaiLy.SelectedValue = 0;
        }
        private void LoadDotGiaoHang()
        {
            String strSql = "select ID, ID& ' - ' & Ngay as DotGiaoHang from GiaoHang";
            DataTable dt = DataAccess.Query(strSql);
            DataRow newRow = dt.NewRow();
            newRow["ID"] = 0;
            newRow["DotGiaoHang"] = "Tất cả";
            dt.Rows.InsertAt(newRow, 0);
            cboDotGiaoHang.ValueMember = "ID";
            cboDotGiaoHang.DisplayMember = "DotGiaoHang";
            cboDotGiaoHang.DataSource = dt;
            cboDotGiaoHang.SelectedValue = 0;
            LoadDaiLy();
        }
        private void LoadThuHoi()
        {
            try
            {
                String strSql = "select th.VoThuVe as MaVoThuHoi, th.VoPhanPhoi as MaVoPhanPhoi, " +
                    "th.ID as ID2, dl.ID as MaDaiLy, dl.TenDaiLy as DaiLy, gas1.TenLoai as VoPhanPhoi, " +
                    "gas.TenLoai as VoThuHoi, th.SoLuong as SoLuong, th.PhiDoiVo as PhiDoiVo," +
                    " day(gh.Ngay)&'/'&month(gh.Ngay)&'/'&year(gh.Ngay) as ThoiGian, gh.ID as MaGH from (((ThuHoi th left join LoaiGas gas on th.VoThuVe=gas.ID) left join LoaiGas gas1 on th.VoPhanPhoi=gas1.ID) left join DaiLy dl on th.DaiLy=dl.ID) left join GiaoHang gh on gh.ID=th.MaGiaoHang";
                if ((int)cboDotGiaoHang.SelectedValue > 0)
                {
                    strSql += " where th.MaGiaoHang =" + (int)cboDotGiaoHang.SelectedValue;
                    if ((int)cboDaiLy.SelectedValue > 0)
                    {
                        strSql += " and dl.ID=" + (int)cboDaiLy.SelectedValue;
                    }
                }
                else
                {
                    if ((int)cboDaiLy.SelectedValue > 0)
                    {
                        strSql += " where dl.ID=" + (int)cboDaiLy.SelectedValue;
                    }
                }
                DataTable dt = DataAccess.Query(strSql);
                dt.Columns.Add("STT");
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    dt.Rows[i]["STT"] = i + 1;
                    if (dt.Rows[i]["MaVoThuHoi"] == System.DBNull.Value)
                        dt.Rows[i]["MaVoThuHoi"] = 0;
                    if (dt.Rows[i]["VoThuHoi"] == System.DBNull.Value)
                        dt.Rows[i]["VoThuHoi"] = "Thế chân";
                }
                BindingSource bs = new BindingSource(dt, null);
                dgvThuHoi.AutoGenerateColumns = false;
                dgvThuHoi.DataSource = bs;           

            }
            catch (Exception ex)
            {
                MessageBox.Show("Không thể load form\r\n" + ex.Message, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                Close();
            }
        }
        private void LoadPhanPhoi()
        {
            try
            {
                String strSql;
                if ((int)cboDotGiaoHang.SelectedValue == 0)
                {
                    if ((int)cboDaiLy.SelectedValue == 0)
                    {
                        strSql = "select dl.ID as MaDaiLy, gas.ID as MaLoaiGas, pp.ID as ID, dl.TenDaiLy as DaiLy, gas.TenLoai as LoaiGas, pp.SoLuong, pp.GiaGoc, pp.GiaBan, day(gh.Ngay)&'/'&month(gh.Ngay)&'/'&year(gh.Ngay) as ThoiGian, gh.ID as MaGH from PhanPhoi pp, DaiLy dl, GiaoHang gh, LoaiGas gas where dl.ID=pp.DaiLy and pp.LoaiGas=gas.ID and pp.MaGiaoHang=gh.ID";
                    }
                    else
                    {
                        strSql = strSql = "select dl.ID as MaDaiLy, gas.ID as MaLoaiGas, pp.ID as ID, dl.TenDaiLy as DaiLy, gas.TenLoai as LoaiGas, pp.SoLuong, pp.GiaGoc, pp.GiaBan, day(gh.Ngay)&'/'&month(gh.Ngay)&'/'&year(gh.Ngay) as ThoiGian, gh.ID as MaGH from PhanPhoi pp, DaiLy dl, GiaoHang gh, LoaiGas gas where dl.ID=pp.DaiLy and pp.LoaiGas=gas.ID and pp.MaGiaoHang=gh.ID and dl.ID = " + (int)cboDaiLy.SelectedValue;
                    }
                }
                else
                {
                    if ((int)cboDaiLy.SelectedValue == 0)
                    {
                        strSql = "select dl.ID as MaDaiLy, gas.ID as MaLoaiGas, pp.ID as ID, dl.TenDaiLy as DaiLy, gas.TenLoai as LoaiGas, pp.SoLuong, pp.GiaGoc, pp.GiaBan, day(gh.Ngay)&'/'&month(gh.Ngay)&'/'&year(gh.Ngay) as ThoiGian, gh.ID as MaGH from PhanPhoi pp, DaiLy dl, GiaoHang gh, LoaiGas gas where dl.ID=pp.DaiLy and pp.LoaiGas=gas.ID and pp.MaGiaoHang=gh.ID and pp.MaGiaoHang = "
                            + (int)cboDotGiaoHang.SelectedValue;
                    }
                    else
                    {
                        strSql = strSql = "select dl.ID as MaDaiLy, gas.ID as MaLoaiGas, pp.ID as ID, dl.TenDaiLy as DaiLy, gas.TenLoai as LoaiGas, pp.SoLuong, pp.GiaGoc, pp.GiaBan, day(gh.Ngay)&'/'&month(gh.Ngay)&'/'&year(gh.Ngay) as ThoiGian, gh.ID as MaGH from PhanPhoi pp, DaiLy dl, GiaoHang gh, LoaiGas gas where dl.ID=pp.DaiLy and pp.LoaiGas=gas.ID and pp.MaGiaoHang=gh.ID and dl.ID = " + (int)cboDaiLy.SelectedValue + " and pp.MaGiaoHang =" + (int)cboDotGiaoHang.SelectedValue;
                    }
                }
                 //strSql = "select dl.ID as MaDaiLy, gas.ID as MaLoaiGas, pp.ID as ID, dl.TenDaiLy as DaiLy, gas.TenLoai as LoaiGas, pp.SoLuong, pp.GiaGoc, pp.GiaBan, day(gh.Ngay)&'/'&month(gh.Ngay)&'/'&year(gh.Ngay) as ThoiGian, gh.ID as MaGH from PhanPhoi pp, DaiLy dl, GiaoHang gh, LoaiGas gas where dl.ID=pp.DaiLy and pp.LoaiGas=gas.ID and pp.MaGiaoHang=gh.ID";
                DataTable dt = DataAccess.Query(strSql);
                dt.Columns.Add("STT");
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    dt.Rows[i]["STT"] = i + 1;
                }
                BindingSource bs = new BindingSource(dt, null);
                dgvPhanPhoi.AutoGenerateColumns = false;
                dgvPhanPhoi.DataSource = bs;
                txtID.Clear();
                txtGiaBan.Clear();
                txtGiaGoc.Clear();

             
            }
            catch (Exception ex)
            {
                MessageBox.Show("Không thể load form\r\n" + ex.Message, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                Close();
            }
        }
        private void frmQLPhanPhoi_Load(object sender, EventArgs e)
        {
            LoadDotGiaoHang();
            LoadPhanPhoi();
            LoadThuHoi();
        }

        private void btnCapNhat_Click(object sender, EventArgs e)
        {
            try
            {
                PhanPhoiDTO phanPhoi = new PhanPhoiDTO();
                phanPhoi.Id = int.Parse(txtID.Text);
                phanPhoi.DaiLy = (int)dgvPhanPhoi.SelectedRows[0].Cells["MaDaiLy"].Value;            
                phanPhoi.GiaGoc = float.Parse(txtGiaGoc.Text);
                phanPhoi.GiaBan = float.Parse(txtGiaBan.Text);
                phanPhoi.MaGiaoHang = (int)dgvPhanPhoi.SelectedRows[0].Cells["MaGH"].Value;
                phanPhoi.LoaiGas = (int)dgvPhanPhoi.SelectedRows[0].Cells["MaLoaiGas"].Value;
                phanPhoi.SoLuong = (int)dgvPhanPhoi.SelectedRows[0].Cells["SoLuong"].Value;
                PhanPhoiDAO.UpdatePhanPhoi(phanPhoi);
                MessageBox.Show("Cập nhật dữ liệu thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                OnLoad(e);
            }
            catch (Exception ex) {
                MessageBox.Show("Cập nhật dữ liệu không thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn có thật sự muốn xóa?", "Thông báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == System.Windows.Forms.DialogResult.Cancel)
            {
                return;
            }
            try
            {
                for (int i = 0; i < dgvPhanPhoi.SelectedRows.Count; i++)
                {
                    int id = (int)dgvPhanPhoi.SelectedRows[i].Cells["ID"].Value;
                    PhanPhoiDAO.DeletePhanPhoi(id);                    
                }
                OnLoad(e);
                MessageBox.Show("Cập nhật dữ liệu thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex) { 
                MessageBox.Show("Cập nhật dữ liệu không thành công\n"+ex.Message, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning); 
            }
        }

        private void dgvPhanPhoi_BindingContextChanged(object sender, EventArgs e)
        {
           
        }

        private void dgvPhanPhoi_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {

        }

        private void dgvPhanPhoi_SelectionChanged(object sender, EventArgs e)
        {
            try
            {
                if (dgvPhanPhoi.SelectedRows.Count > 0)
                {
                    txtID.Text = dgvPhanPhoi.SelectedRows[0].Cells["ID"].Value.ToString();
                    txtGiaGoc.Text = dgvPhanPhoi.SelectedRows[0].Cells["GiaGoc"].Value.ToString();
                    txtGiaBan.Text = dgvPhanPhoi.SelectedRows[0].Cells["GiaBan"].Value.ToString();                 
                 
                    btnCapNhat.Enabled = true;
                }
                else
                {
                    btnCapNhat.Enabled = false;
                }
            }
            catch (Exception ex) {
                MessageBox.Show(ex.Message, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            frmPhanPhoi frm = new frmPhanPhoi();
            frm.ShowDialog();
            OnLoad(e);
        }

        private void cboDotGiaoHang_SelectedIndexChanged(object sender, EventArgs e)
        {
           
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            Close();
        }
        private String KiemTraThuVo()
        {
            String mess = "";
            if (txtPhiDoiVo.Text == "")
            {
                mess = "Dữ liệu nhập vào chưa đầy đủ.";
                return mess;
            }
            int rs;
            String temp = "";
            if (Int32.TryParse(txtPhiDoiVo.Text, out rs) == false)
            {
                temp += "Phí đổi vỏ";
            }
            if (temp != "")
            {
                mess = "Dữ liệu " + temp + " không đúng";
                return mess;
            }
           
            return mess;
        }
        private void btnCapNhat_ThuVo_Click(object sender, EventArgs e)
        {
            String strError = KiemTraThuVo();
            if (strError != "")
            {
                MessageBox.Show(strError, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            ThuHoiDTO thuhoiDTO = new ThuHoiDTO()
            {
                Id = Int32.Parse(txtID2.Text),
                DaiLy = (int)dgvThuHoi.SelectedRows[0].Cells["MaDaiLy"].Value,
                VoPhanPhoi = (int)dgvThuHoi.SelectedRows[0].Cells["MaVoPhanPhoi"].Value,
                VoThuVe = (int)dgvThuHoi.SelectedRows[0].Cells["MaThuHoi"].Value,
                PhiDoiVo = Int32.Parse(txtPhiDoiVo.Text),
                SoLuong = (int)dgvThuHoi.SelectedRows[0].Cells["SoLuong"].Value,
                MaGiaoHang = (int)dgvThuHoi.SelectedRows[0].Cells["MaGH"].Value
            };
            try
            {
                ThuHoiDAO.UpdateThuHoi(thuhoiDTO);
                MessageBox.Show("Cập nhật dữ liệu thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                OnLoad(e);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("Cập nhật dữ liệu không thành công\n" + ex.Message, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnXoa_ThuVo_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn có thật sự muốn xóa không?", "Thông báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == System.Windows.Forms.DialogResult.Cancel)
            {
                return;
            }
            try
            {
                int id = Int32.Parse(txtID2.Text);
                ThuHoiDAO.DeleteThuHoi(id);
                MessageBox.Show("Cập nhật dữ liệu thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                OnLoad(e);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("Cập nhật dữ liệu không thành công\n" + ex.Message, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnThem_ThuVo_Click(object sender, EventArgs e)
        {
            ThuVo frm = new ThuVo();
            frm.ShowDialog();
            OnLoad(e);
        }

        private void dgvThuHoi_SelectionChanged(object sender, EventArgs e)
        {
            if (dgvThuHoi.SelectedRows.Count > 0)
            {
                txtID2.Text = dgvThuHoi.SelectedRows[0].Cells["ID2"].Value.ToString();              
                txtPhiDoiVo.Text = dgvThuHoi.SelectedRows[0].Cells["PhiDoiVo"].Value.ToString();
                btnCapNhat_ThuVo.Enabled = true;                                
            }
            else
            {
                btnCapNhat_ThuVo.Enabled = false;
            }
        }

        private void cboDaiLy_SelectedIndexChanged(object sender, EventArgs e)
        {
          
        }

        private void cboDotGiaoHang_SelectionChangeCommitted(object sender, EventArgs e)
        {
            LoadDaiLy();
            LoadPhanPhoi();
            LoadThuHoi();
        }

        private void cboDaiLy_SelectionChangeCommitted(object sender, EventArgs e)
        {
            LoadPhanPhoi();
            LoadThuHoi();
        }

        private void frmQLPhanPhoi_SizeChanged(object sender, EventArgs e)
        {
            dgvPhanPhoi.Height = (Height - 220) / 2;
            dgvThuHoi.Location = new Point(dgvThuHoi.Location.X, dgvPhanPhoi.Location.Y + dgvPhanPhoi.Height + 60);
            dgvThuHoi.Height = (Height - 220) / 2;
            lblThuHoi.Location = new Point(dgvThuHoi.Location.X, dgvPhanPhoi.Location.Y + dgvPhanPhoi.Height + 40);
            lblID2.Location = new Point(dgvThuHoi.Location.X, dgvPhanPhoi.Location.Y + dgvPhanPhoi.Height + 10);
            txtID2.Location = new Point(lblID2.Location.X + lblID2.Width + 10, dgvPhanPhoi.Location.Y + dgvPhanPhoi.Height + 10);
            lblPhiDoiVo.Location = new Point(txtID2.Location.X + txtID2.Width + 10, dgvPhanPhoi.Location.Y + dgvPhanPhoi.Height + 10);
            txtPhiDoiVo.Location = new Point(lblPhiDoiVo.Location.X + lblPhiDoiVo.Width + 10, dgvPhanPhoi.Location.Y + dgvPhanPhoi.Height + 10);
            btnCapNhat_ThuVo.Location = new Point(txtPhiDoiVo.Location.X + txtPhiDoiVo.Width + 50, dgvPhanPhoi.Location.Y + dgvPhanPhoi.Height + 10);
        }
    }
}
